home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / patch / inp.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  9KB  |  334 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. /* $Header: /home/dice/com/src/patch/RCS/inp.c,v 30.8 1994/08/18 05:51:53 dice Exp dice $
  7.  *
  8.  * $Log: inp.c,v $
  9.  * Revision 30.8  1994/08/18  05:51:53  dice
  10.  * .
  11.  *
  12.  * Revision 30.0  1994/06/10  18:08:49  dice
  13.  * .
  14.  *
  15.  * Revision 30.0  1994/06/10  18:08:49  dice
  16.  * .
  17.  *
  18.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  19.  * patch10: made a little smarter about sccs files
  20.  * 
  21.  * Revision 2.0  86/09/17  15:37:02  lwall
  22.  * Baseline for netwide release.
  23.  * 
  24.  */
  25.  
  26. #include "EXTERN.h"
  27. #include "common.h"
  28. #include "util.h"
  29. #include "pch.h"
  30. #include "INTERN.h"
  31. #include "inp.h"
  32.  
  33. /* Input-file-with-indexable-lines abstract type */
  34.  
  35. static long i_size;            /* size of the input file */
  36. static char *i_womp;            /* plan a buffer for entire file */
  37. static char **i_ptr;            /* pointers to lines in i_womp */
  38.  
  39. static int tifd = -1;            /* plan b virtual string array */
  40. static char *tibuf[2];            /* plan b buffers */
  41. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  42. static LINENUM lines_per_buf;        /* how many lines per buffer */
  43. static int tireclen;            /* length of records in tmp file */
  44.  
  45. /* New patch--prepare to edit another file. */
  46.  
  47. void
  48. re_input()
  49. {
  50.     if (using_plan_a) {
  51.     i_size = 0;
  52. #ifndef lint
  53.     if (i_ptr != Null(char**))
  54.         free((char *)i_ptr);
  55. #endif
  56.     if (i_womp != Nullch)
  57.         free(i_womp);
  58.     i_womp = Nullch;
  59.     i_ptr = Null(char **);
  60.     }
  61.     else {
  62.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  63.     Close(tifd);
  64.     tifd = -1;
  65.     free(tibuf[0]);
  66.     free(tibuf[1]);
  67.     tibuf[0] = tibuf[1] = Nullch;
  68.     tiline[0] = tiline[1] = -1;
  69.     tireclen = 0;
  70.     }
  71. }
  72.  
  73. /* Constuct the line index, somehow or other. */
  74.  
  75. void
  76. scan_input(filename)
  77. char *filename;
  78. {
  79.     if (!plan_a(filename))
  80.     plan_b(filename);
  81.     if (verbose) {
  82.     say3("Patching file %s using Plan %s...\n", filename,
  83.       (using_plan_a ? "A" : "B") );
  84.     }
  85. }
  86.  
  87. /* Try keeping everything in memory. */
  88.  
  89. bool
  90. plan_a(filename)
  91. char *filename;
  92. {
  93.     int ifd;
  94.     Reg1 char *s;
  95.     Reg2 LINENUM iline;
  96.  
  97.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  98.     if (verbose)
  99.         say2("(Creating file %s...)\n",filename);
  100.     makedirs(filename, TRUE);
  101.     close(creat(filename, 0666));
  102.     }
  103.     if (stat(filename, &filestat) < 0) {
  104.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  105.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  106.         Sprintf(buf, CHECKOUT, filename);
  107.         if (verbose)
  108.         say2("Can't find %s--attempting to check it out from RCS.\n",
  109.             filename);
  110.         if (system(buf) || stat(filename, &filestat))
  111.         fatal2("Can't check out %s.\n", filename);
  112.     }
  113.     else {
  114.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  115.         if (stat(s=buf+20, &filestat) >= 0 ||
  116.           stat(s=buf+25, &filestat) >= 0) {
  117.         Sprintf(buf, GET, s);
  118.         if (verbose)
  119.             say2("Can't find %s--attempting to get it from SCCS.\n",
  120.             filename);
  121.         if (system(buf) || stat(filename, &filestat))
  122.             fatal2("Can't get %s.\n", filename);
  123.         }
  124.         else
  125.         fatal2("Can't find %s.\n", filename);
  126.     }
  127.     }
  128.     filemode = filestat.st_mode;
  129.     if ((filemode & S_IFMT) & ~S_IFREG)
  130.     fatal2("%s is not a normal file--can't patch.\n", filename);
  131.     i_size = filestat.st_size;
  132.     if (out_of_mem) {
  133.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  134.     out_of_mem = FALSE;
  135.     return FALSE;            /* force plan b because plan a bombed */
  136.     }
  137. #ifdef lint
  138.     i_womp = Nullch;
  139. #else
  140.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  141.                     /* i_size, but that's okay, I think. */
  142. #endif
  143.     if (i_womp == Nullch)
  144.     return FALSE;
  145.     if ((ifd = open(filename, 0)) < 0)
  146.     fatal2("Can't open file %s\n", filename);
  147. #ifndef lint
  148.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  149.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  150.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  151.     return FALSE;    /*   undersized. */
  152.     }
  153. #endif
  154.     Close(ifd);
  155.     if (i_size && i_womp[i_size-1] != '\n')
  156.     i_womp[i_size++] = '\n';
  157.     i_womp[i_size] = '\0';
  158.  
  159.     /* count the lines in the buffer so we know how many pointers we need */
  160.  
  161.     iline = 0;
  162.     for (s=i_womp; *s; s++) {
  163.     if (*s == '\n')
  164.         iline++;
  165.     }
  166. #ifdef lint
  167.     i_ptr = Null(char**);
  168. #else
  169.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  170. #endif
  171.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  172.     free((char *)i_womp);
  173.     return FALSE;
  174.     }
  175.     
  176.     /* now scan the buffer and build pointer array */
  177.  
  178.     iline = 1;
  179.     i_ptr[iline] = i_womp;
  180.     for (s=i_womp; *s; s++) {
  181.     if (*s == '\n')
  182.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  183.     }
  184.     input_lines = iline - 1;
  185.  
  186.     /* now check for revision, if any */
  187.  
  188.     if (revision != Nullch) { 
  189.     if (!rev_in_string(i_womp)) {
  190.         if (force) {
  191.         if (verbose)
  192.             say2(
  193. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  194.             revision);
  195.         }
  196.         else {
  197.         ask2(
  198. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  199.             revision);
  200.         if (*buf != 'y')
  201.         fatal1("Aborted.\n");
  202.         }
  203.     }
  204.     else if (verbose)
  205.         say2("Good.  This file appears to be the %s version.\n",
  206.         revision);
  207.     }
  208.     return TRUE;            /* plan a will work */
  209. }
  210.  
  211. /* Keep (virtually) nothing in memory. */
  212.  
  213. void
  214. plan_b(filename)
  215. char *filename;
  216. {
  217.     Reg3 FILE *ifp;
  218.     Reg1 int i = 0;
  219.     Reg2 int maxlen = 1;
  220.     Reg4 bool found_revision = (revision == Nullch);
  221.  
  222.     using_plan_a = FALSE;
  223.     if ((ifp = fopen(filename, "r")) == Nullfp)
  224.     fatal2("Can't open file %s\n", filename);
  225.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  226.     fatal2("Can't open file %s\n", TMPINNAME);
  227.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  228.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  229.         found_revision = TRUE;
  230.     if ((i = strlen(buf)) > maxlen)
  231.         maxlen = i;            /* find longest line */
  232.     }
  233.     if (revision != Nullch) {
  234.     if (!found_revision) {
  235.         if (force) {
  236.         if (verbose)
  237.             say2(
  238. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  239.             revision);
  240.         }
  241.         else {
  242.         ask2(
  243. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  244.             revision);
  245.         if (*buf != 'y')
  246.             fatal1("Aborted.\n");
  247.         }
  248.     }
  249.     else if (verbose)
  250.         say2("Good.  This file appears to be the %s version.\n",
  251.         revision);
  252.     }
  253.     Fseek(ifp, 0L, 0);        /* rewind file */
  254.     lines_per_buf = BUFFERSIZE / maxlen;
  255.     tireclen = maxlen;
  256.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  257.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  258.     if (tibuf[1] == Nullch)
  259.     fatal1("Can't seem to get enough memory.\n");
  260.     for (i=1; ; i++) {
  261.     if (! (i % lines_per_buf))    /* new block */
  262.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  263.         fatal1("patch: can't write temp file.\n");
  264.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  265.       == Nullch) {
  266.         input_lines = i - 1;
  267.         if (i % lines_per_buf)
  268.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  269.             fatal1("patch: can't write temp file.\n");
  270.         break;
  271.     }
  272.     }
  273.     Fclose(ifp);
  274.     Close(tifd);
  275.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  276.     fatal2("Can't reopen file %s\n", TMPINNAME);
  277.     }
  278. }
  279.  
  280. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  281.  
  282. char *
  283. ifetch(line,whichbuf)
  284. Reg1 LINENUM line;
  285. int whichbuf;                /* ignored when file in memory */
  286. {
  287.     if (line < 1 || line > input_lines)
  288.     return "";
  289.     if (using_plan_a)
  290.     return i_ptr[line];
  291.     else {
  292.     LINENUM offline = line % lines_per_buf;
  293.     LINENUM baseline = line - offline;
  294.  
  295.     if (tiline[0] == baseline)
  296.         whichbuf = 0;
  297.     else if (tiline[1] == baseline)
  298.         whichbuf = 1;
  299.     else {
  300.         tiline[whichbuf] = baseline;
  301. #ifndef lint        /* complains of long accuracy */
  302.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  303. #endif
  304.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  305.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  306.     }
  307.     return tibuf[whichbuf] + (tireclen*offline);
  308.     }
  309. }
  310.  
  311. /* True if the string argument contains the revision number we want. */
  312.  
  313. bool
  314. rev_in_string(string)
  315. char *string;
  316. {
  317.     Reg1 char *s;
  318.     Reg2 int patlen;
  319.  
  320.     if (revision == Nullch)
  321.     return TRUE;
  322.     patlen = strlen(revision);
  323.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  324.     return TRUE;
  325.     for (s = string; *s; s++) {
  326.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  327.         isspace(s[patlen+1] )) {
  328.         return TRUE;
  329.     }
  330.     }
  331.     return FALSE;
  332. }
  333.  
  334.